import { request } from '@umijs/max';
import { Page, Result } from '../API';

export interface GameItem {
  gameId: string;
  url: string; //宣传图图片
  name: string; //赛事
  address: string;//比赛地址
  person: string;//联系人
  phone: string;//联系电话
  offTime: string; //报名截止时间
  startTime: string; //开赛时间
  endTime: string; //结束时间
  createTime: string; //创建时间
  status: number; //1-报名中  2-进行中 3-已结束 4已取消
  offSize: number; //报名人数
  allSize: number; //总人数
  range: number; //公开范围 1 公开赛
  category: number; //比赛类别 1 双人赛
  type: number; //1 积分晋级赛
}
export interface GameInfo {
  gameId: string;
  url: string[]; //宣传图图片
  name: string; //赛事
  address: string;//比赛地址
  person: string;//联系人
  phone: string;//联系电话
  offTime: string; //报名截止时间
  startTime: string; //开赛时间
  endTime: string; //结束时间
  createTime: string; //创建时间
  status: number; //1-报名中(默认值)  2-进行中 3-已结束 4已取消
  offSize: number; //报名人数
  signleOffSize: number;//报名签到人数
  allSize: number; //总人数
  range: number; //公开范围 1 公开赛(默认值)
  category: number; //比赛类别 1 双人赛(默认值)
  type: number; //1 积分晋级赛(默认值)
  ruleJson: string;//赛制规则 json格式字符串
  introduce: string;//赛事介绍
  commRoules: number;//是否使用通用规则 1 使用(默认值)
  singleOff: number//允许单人报名 1 允许(默认值)
}
/**
 * 赛制规则
 */
export interface GameRules {
  rulesId: string;
  type: number;//类型 1 积分晋级赛制
  statue: number;//状态 1 正常 2 禁用 
  name: string; //名称
  json: string;//规则json 字符
}

/**
 * 赛制规则-积分晋级赛
 */
export interface GameRulesTypeA {
  basicPoints: number;//基础规则-基础积分
  promotePoints: number;//基础规则-晋级积分
  rankingCheck: number;//排名规则-积分选项1 积分 2用时 3净胜场
  pointsRatio: number;//积分规则-比例值 0~1
  abstainTime: number; //弃权规则-胜方比赛用时 
  outPoints: number;//淘汰规则
}
/**
 * 赛事详情-选手
 */
export interface GamePlayer{
  playerId: string;//选手
  playerName: string;//选手
  playerPhonse: string;//手机
  playerState: number;//状态 1-已签到  否则未签到
  playerTeamId: string;//所属队伍Id
}
/**
 * 赛事详情-队伍
 */
export interface GameTeam{
  teamId: string;//团队Id
  teamCode: string;//团队编号
  teamPoints: number;//团队积分
  teamPerson: GamePlayer;  //团队员成员
  teamPerson2: GamePlayer;  //团队成员
}
/**
 * 赛事详情-积分排名
 */
export interface GamePointRank {
  rankIndex: number;//最终排名
  gameId: string;//赛事id
  state: number;//状态 1-比赛中  2-已晋级 3-淘汰
  team:GameTeam
}
/**
 * 赛事详情-队伍PK
 */
export interface GameTeamPK {
  pkId: string;//PKid
  gameId: string;//赛事id
  pkState: number;//1-进行中  2-已结束 3-已取消
  pkTabNo: string;//桌号
  pkPoints: number;//结算积分
  pkRoundCount: number;//pk 轮次
  pkWinTeamId: number;//胜出队伍Id
  pkTeamAId: string;//A组Id 
  pkTeamCode: string;//A组团队编号
  pkTeamCode2: string;
  pkTeamA: GameTeam;//A组 
  pkTeamBId: string;//B组Id
  pkTeamB: GameTeam;//B组
  pkEast: GamePlayer;//方位东选手
  pkWest: GamePlayer;//方位西选手
  pkSouth: GamePlayer;///方位南选手
  pkNorth: GamePlayer;///方位北选手
}


//获取赛事积分排名
export async function queryGamePointRank(gameId: string) {
  return request<Result<GamePointRank[]>>('/pc/game/points/rank', {
    method: 'GET',
    params: {gameId:gameId},
  });
}

//获取通用规则
export async function queryCommGameRules() {
  return request<Result<string>>('/pc/game/rules/queryCommRules', {
    method: 'GET',
    params: {},
  });
}

//编辑通用规则
export async function editCommGameRules(params: { txt?: string }) {
  return request<Result<Boolean>>('/pc/game/rules/editCommRules', {
    method: 'POST',
    params: { ...params },
  });
}


//获取赛制
export async function queryGameRules(params: {
  type: number
}) {
  return request<Result<GameRules>>('/pc/game/rules/queryRules', {
    method: 'GET',
    params: { ...params },
  });
}


//编辑赛制规则
export async function editGameRules(params: {
  rulesId: string,
  data: string
}) {
  return request<Result<Boolean>>('/pc/game/rules/editRules', {
    method: 'POST',
    params: { ...params },
  });
}

//更新赛制状态
export async function updateGameRulesState(params: {
  rulesId: string,
  state: number
}) {
  return request<Result<Boolean>>('/pc/game/rules/updateState', {
    method: 'POST',
    params: { ...params },
  });
}


//查询赛制列表
export async function queryPageGameRules(params: {
  current?: number;
  pageSize?: number;
}) {
  return request<Result<Page<GameRules>>>('/pc/game/rules/page', {
    method: 'GET',
    params: { ...params },
  });
}

/**
 * 新增赛事
 * @param params
 * @returns
 */
export async function newGame(params: GameInfo) {
  return request<Result<Boolean>>('/pc/game/newInfo', {
    method: 'POST',
    params: { ...params },
  });
}
/**
 * 编辑赛事
 * @param params
 * @returns
 */
export async function editGame(params: GameInfo) {
  return request<Result<Boolean>>('/pc/game/editInfo', {
    method: 'POST',
    params: { ...params },
  });
}

/**
 * 查询赛事详情
 * @param params
 * @returns
 */
export async function queryGameInfo(gameId: string) {
  return request<Result<GameInfo>>('/pc/game/info', {
    method: 'GET',
    params: { gameId: gameId },
  });
}

/**
 * 删除赛事
 * @param params
 * @returns
 */
export async function deleteItem(gameId: string) {
  return request<Result<boolean>>('/pc/game/delete', {
    method: 'POST',
    params: { gameId: gameId },
  });
}

/**
 * 更新赛事状态
 * @param params state 1-报名中  2-进行中 3-已结束 4已取消
 * @returns
 */
export async function updateStateItem(gameId: string, state: number) {
  return request<Result<boolean>>('/pc/game/updateState', {
    method: 'POST',
    params: { gameId: gameId, state: state },
  });
}
/**
 * 查询当天赛事信息
 * @param params
 * @returns
 */
export async function queryNowGame() {
  return request<Result<GameItem>>('/pc/game/now', {
    method: 'GET',
    params: {},
  });
}

/**
 * 赛事列表分页
 * @param params
 * @returns
 */
export async function queryPageGame(params: {
  name?: string;
  status?: number;
  type?: number;
  current?: number;
  pageSize?: number;
}) {
  return request<Result<Page<GameItem>>>('/pc/game/page', {
    method: 'GET',
    params: { ...params },
  });
}
